import com.baidu.echarts.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hdfs.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@WebServlet("/useraction")
public class UserActionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "text/json;charset=utf-8");
//        resp.getWriter().write("{\"series\":{\"name\": \"访问来源\",\"type\": \"pie\",\"radius\": \"55%\",\"center\": [\"50%\", \"60%\"],\"data\": [{\"value\": 335,\"name\": \"直接访问\"},{\"value\": 310,\"name\": \"邮件营销\"},{\"value\": 234,\"name\": \"联盟广告\"},{\"value\": 135,\"name\": \"视频广告\"},{\"value\": 1548,\"name\": \"搜索引擎\"}]},\"legend\":[\"直接访问\", \"邮件营销\", \"联盟广告\", \"视频广告\", \"搜索引擎\"]}");
        resp.getWriter().write(testEcharts());
    }



    private String testEcharts() throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        Option option = new Option();


        Map<Integer, String[]> SS= FileUtil.ReadPartFile("D:\\userActionoutput\\part-r-00000");
        //Map<Integer, String[]> SS= FileUtil.ReadPartFile("http://node-1:50070/explorer.html#/user/output/project/p6/part-r-00000");
        String[] line0=SS.get(0);     //第0行
        String[] line1=SS.get(1);     //第1行
        String[] line2=SS.get(2);     //第2行
        String[] line3=SS.get(3);     //第3行
        String cell00=line0[0];        //第0行的第0个数据
        String cell01=line0[1];        //第0行的第1个数据
        String cell10=line1[0];        //第1行的第0个数据
        String cell11=line1[1];        //第1行的第1个数据
        String cell20=line2[0];        //第2行的第0个数据
        String cell21=line2[1];        //第2行的第1个数据
        String cell30=line3[0];        //第3行的第0个数据
        String cell31=line3[1];        //第3行的第1个数据


        Title title = new Title();
        title.setText("指定类型商品被用户操作的类型及次数");
        title.setSubtext("商品类型：13926");
        title.setX("center");
        option.setTitle(title);

        Tooltip tooltip = new Tooltip();
        tooltip.setTrigger("item");
        tooltip.setFormatter("{a} <br/>{b} : {c} ({d}%)");
        option.setTooltip(tooltip);

        Legend legend = new Legend();
        legend.setOrient("vertical");
        //点击、收藏、加购物车、支付
        legend.setData(Arrays.asList(new String[]{"点击", "收藏", "加入购物车", "支付"}));
        legend.setX("left");
        option.setLegend(legend);

        option.setCalculable(true);

        Series series = new Series();
        series.setName("用户操作");
        series.setCenter(Arrays.asList(new String[]{"50%", "60%"}));
        series.setRadius("50%");
        series.setType("pie");


        List<Series> seriesList = new ArrayList<>();
        List<SeriesData> data = new ArrayList<>();
        SeriesData data1 = new SeriesData();
        data1.setName("点击");
        data1.setValue(cell01);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("收藏");
        data1.setValue(cell11);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("加入购物车");
        data1.setValue(cell21);
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("支付");
        data1.setValue(cell31);
        data.add(data1);


        series.setData(data);
        seriesList.add(series);
        option.setSeries(seriesList);

        Toolbox toolbox = new Toolbox();
        toolbox.setShow(true);
        Feature feature = new Feature();
        feature.setMark(new MarkItem(true));
        feature.setRestore(new MarkItem(true));
        feature.setSaveAsImag(new MarkItem(true));
        feature.setDataView(new DataView(true, true));

        toolbox.setFeature(feature);
        option.setToolbox(toolbox);

        System.out.println(mapper.writeValueAsString(option));
        return mapper.writeValueAsString(option);
    }

}
